﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DepartmentOnline.Models;
using BasicFrame.WebControls;

namespace DepartmentOnline
{
    public partial class Person : System.Web.UI.Page
    {
        private int CurrentID;
        public string CurrentName;
        public Instructor CurrentPerson;
        public const string def = @"images/users/empty_photo.gif";
        protected void Page_Load(object sender, EventArgs e)
        {
            CurrentPerson = new Instructor {Photo = def};
            
               if(Session["PersonID"] != null)
               {
                   CurrentID = Convert.ToInt32(Session["PersonID"]); 
                   using (var context = new departmentEntities())
                   {
                       var person = context.Instructors.Where(x => x.Id == CurrentID);
                       var firstOrDefault = person.FirstOrDefault();
                       if (firstOrDefault != null)
                       {
                           CurrentName = firstOrDefault.Fio;
                           CurrentPerson = firstOrDefault;
						   if(CurrentPerson.Photo != null)
								imgPhoto.ImageUrl = CurrentPerson.Photo;
                           lbDescription.Text = CurrentPerson.Description;

                           ReloadPublGrid();

                           ReloadTaskGrid();
                       }
                   }
               }
        }

        private void ReloadTaskGrid()
        {
            using (var context = new departmentEntities())
            {
                context.Tasks.Where(x => x.Instructor.Id == CurrentID).Load();
                TaskGrid.DataSource = context.Tasks.Local;
                TaskGrid.DataBind();
            }
        }

        private void ReloadPublGrid()
        {
            using (var context = new departmentEntities())
            {
                context.Publications.Where(x => x.Instructor.Id == CurrentID).Load();
                PublGrid.DataSource = context.Publications.Local;
                PublGrid.DataBind();
            }
        }

        protected void AddTask(object sender, EventArgs e)
        {
            if(TTopicTB.Text.Trim().Length > 0 && TDescTB.Text.Trim().Length > 0)
            {
                using (var context = new departmentEntities())
                {
                    if (CurrentPerson != null)
                    {
                        try
                        {
                            var curr = Convert.ToInt32(Session["PersonID"]);
                            var newObj = new DepartmentOnline.Models.Task()
                                             {
                                                 Description = TDescTB.Text,
                                                 Instructor = context.Instructors.FirstOrDefault(x=> x.Id == curr),
                                                 IsComplete = false,
                                                 Topic = TTopicTB.Text
                                             };
                            context.Tasks.Add(newObj);
                            context.SaveChanges();
                            ReloadTaskGrid();
                        }
                        catch (DbEntityValidationException dbEx)
                        {
                            foreach (var validationErrors in dbEx.EntityValidationErrors)
                            {
                                foreach (var validationError in validationErrors.ValidationErrors)
                                {
                                    System.Diagnostics.Trace.WriteLine(validationError.PropertyName + " " + validationError.ErrorMessage);
                                }
                            }
                        }
                    }
                }
            }
        }

        protected void AddPublication(object sender, EventArgs e)
        {
            if (PNameTB.Text.Trim().Length > 0)
            {
                using (var context = new departmentEntities())
                {
                    if (CurrentPerson != null)
                    {
                        try{
                            var curr = Convert.ToInt32(Session["PersonID"]);
                        var newObj = new DepartmentOnline.Models.Publication()
                        {
                            Name = PNameTB.Text,
                            Instructor = context.Instructors.FirstOrDefault(x => x.Id == CurrentID),
                            Date = PDatePicker.SelectedDate
                        };
                        context.Publications.Add(newObj);
                        context.SaveChanges();
                            ReloadPublGrid();
                         }
                        catch (DbEntityValidationException dbEx)
                        {
                            foreach (var validationErrors in dbEx.EntityValidationErrors)
                            {
                                foreach (var validationError in validationErrors.ValidationErrors)
                                {
                                    System.Diagnostics.Trace.WriteLine(validationError.PropertyName + " " + validationError.ErrorMessage);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}